function CategListRefresh() {
    $.ajax({
        url: "/Category", // A faire
        type: 'GET',
        dataType: 'json',
        data: {},
        success: function (myData) {
            $('p.itemcateg').remove();
            $('#myTable').remove();
            $('<p style="margin-top:15%">').html("<center><a title=\"Documentation Utilisateurs\" href=\"#\" onclick=\"CategDetailRefresh(" + myData[0].Id + ");\" id=\"" + myData[0].Id + "\"><img alt=\"Documentation Utilisateurs\" src=\"/Content/images/docuti.png\" /></a><a title=\"Documentation Developpement\" href=\"#\" onclick=\"CategDetailRefresh(" + myData[1].Id + ");\"id=\"" + myData[1].Id + "\"><img alt=\"Documentation Developpement\" src=\"/Content/images/docdev.png\" /></a><a title=\"Documentation Technique\" href=\"#\" onclick=\"CategDetailRefresh(" + myData[2].Id + ");\" class=\"loadTech\" id=\"" + myData[2].Id + "\" ><img alt=\"Documentation Technique\" src=\"/Content/images/doctech.png\" /></a></center></p>")
			        .addClass('itemcateg')
                    .appendTo($('ul.itemcateg'))
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
    });
}

function CategDetailRefresh(id) {
    $.ajax({

        url: "/Resource",
        type: 'GET',
        dataType: 'json',
        data: {},
        success: function (myData) {
            $('p.itemcateg').remove();
            $('#myTable').remove();
            $('<p>').html("<center><img alt=\"Documentation Utilisateurs\" src=\"Content/images/doc-" + id + ".png\" width=\"50%\" height=\"50%\"></center>").addClass('itemcateg').appendTo($('ul.itemcateg'))
            $('<p>').html("<a href='#' id='add-file-button'><img src='Content/images/ressourceplus.png'/></a> <br/><input type=\"hidden\" id=\"hidden-categ\" value=" + id + " />").addClass('itemcateg').appendTo($('ul.itemcateg'))
            $('<table id=\"myTable\">').html("<thead><tr><th>Nom</th><th>Taille</th><th>Supprimer</th></tr></thead><tbody>").addClass('tablesorter').appendTo($('ul.itemcateg'))
            for (var i = 0; i < myData.length; i++) {
                if (myData[i].Category.Id == id) {
                    $('<tr>').html("<td><li class=\"file ext_"+myData[i].File.Path.substr((myData[i].File.Path.lastIndexOf('.') + 1))+"\"><a target=\"_blank\" href=\"" + myData[i].File.Path + "\">"+myData[i].Label + "</a></td><td> " + myData[i].File.Size/1000 + " Ko</td><td>  <img src=\"/Content/images/docdev/delete.ico\" class=\"itemd\" onClick=\"CategDeleteRefresh(" + id + "," + myData[i].Id + ");\" id=\"" + id + "\"></td></li>")
                    .addClass('itemcateg')
                    .appendTo($('table.tablesorter'))
                }
            }
            $('<p>').html("</tbody></table><div id=\"file-uploader\"><noscript><p>Please enable JavaScript to use file uploader.</p></noscript></div>").addClass('itemcateg').appendTo($('ul.itemcateg'))

            $('#add-file-button').click(function () {
                $('#add-resource').dialog('open');
            });
            $("#myTable").tablesorter({ sortList: [[0, 0], [1, 0]] }); 
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
    });
}
function CategDeleteRefresh(idc, idr) {
    $.ajax({
        url: "/Resource/Delete", // A faire
        type: 'DELETE',
        dataType: 'json',
        data: {
            'Id': idr
        },
        success: function (myData) {
            CategDetailRefresh(idc);
        },
        error: function (jqXHR, textStatus, errorThrown) {
        }
    });
   
}
$(document).ready(function () {

   
    $("body").append('<div id="add-resource" title="Ajout fichier"></div>');

    $("#add-resource").html('<FORM id="taskEditForm" METHOD="POST" ACTION="#" class="jqtransform"> Titre :<INPUT TYPE="text" id=\"fileTitle\" SIZE="30"><br /> Description:<INPUT TYPE="text" id=\"fileDescription\" SIZE="30"><input type="hidden" id="taskIdHidden"><br /><div id=\"file-uploader\"><noscript><p>Please enable JavaScript to use file uploader.</p></noscript></div></form>');
    var $dialog = $('#add-resource').dialog({
        autoOpen: false,
        title: 'Ajout d\'une ressource',
        width: 500,
        height: 300
    });
    var uploader = new qq.FileUploader({
        element: document.getElementById('file-uploader'),
        action: '/File/Create',
        params: {},
        allowedExtensions: [],
        sizeLimit: 0,
        minSizeLimit: 0, // min size
        debug: false,
        onSubmit: function (id, fileName) { },
        onProgress: function (id, fileName, loaded, total) { },
        onComplete: function (id, fileName, responseJSON) {
            if($('#fileTitle').val() == "") alert("La ressource doit avoir un titre");
            else{
                $.ajax({
                    url: "/Resource/Create",
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        'Label': $('#fileTitle').val(),
                        'UserId': userGetSessionDetails().UserId,
                        'CategoryId': $('#hidden-categ').val(), // recup val champ hidden
                        'Description': $('#fileDescription').val(),
                        'FileId': responseJSON['file']['Id']
                },
                success: function (myData) {
                    CategDetailRefresh($('#hidden-categ').val());
                    $('#add-resource').dialog('close');
                },
                error: function (jqXHR, textStatus, errorThrown) {
                 
                }
            });
        }
    },
    onCancel: function (id, fileName) { },
    messages: {},
    showMessage: function (message) { alert(message); }
});
});
